<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class FixRequest
{
    /**
     * Name: 特殊情况下，修复 request 中 json 解析失败的问题
     * User: Lark
     * Data: 2022/7/15
     * Time: 14:32
     *
     * @param  Request  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $request->headers->set('accept', 'application/json');
        $raw = urldecode(file_get_contents('php://input'));
        $data = (array) json_encode(substr($raw, 1), 1);
        if (!empty($data)) {
            $request->merge($data);
        }

        return $next($request);
    }
}
